//
//  ViewController.m
//  OpenThirdAppDemo
//
//  Created by Victor on 2018/7/20.
//  Copyright © 2018 Victor. All rights reserved.
//

#import "ViewController.h"
#import <Foundation/Foundation.h>
#import "OpenFileWithApplication.h"

NSString *kOpenFileWithApplictionPath = @"kOpenFileWithApplictionPath";
NSString *kOpenFileWithFilePathArray = @"kOpenFileWithFilePathArr";



@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

- (IBAction)chooseApp:(id)sender {
    NSURL *appUrl = [self chooseFile];

    [_appPathField setStringValue:appUrl.path];

}

- (IBAction)chooseFile:(id)sender {
    NSURL *filePath = [self chooseFile];

    [_filePathField setStringValue:filePath.path];

}


- (NSURL *)chooseFile
{
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setAllowsMultipleSelection:false];  //是否允许多选file
    NSInteger finded = [panel runModal]; //获取panel的响应

    if (finded == NSModalResponseOK) {
        return [panel URL];
    }else{
        return nil;
    }
}




//////////////////////////////////////////////////////////////

#pragma mark - 第一种方法

- (IBAction)useShell:(id)sender
{
    //[self invokingShellScriptAtPath:@"/Users/victor/Desktop/ShellStudy/OpenFile.sh" ];

    NSArray *paths = [NSArray arrayWithObjects:_filePathField.stringValue,nil];
    NSString *appPath = _appPathField.stringValue;

    NSString *shellPath = [[NSBundle mainBundle] pathForResource:@"OpenFile" ofType:@"sh"];

    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    [dic setValue:paths forKey:kOpenFileWithFilePathArray];
    [dic setValue:appPath forKey:kOpenFileWithApplictionPath];
    [self excuteShellWithParams:dic shellScriptPath:shellPath];
}

/*
 Command: ./OpenFile.sh [-a <application>] [filenames] [--args arguments]
 */
-(id)excuteShellWithParams:(NSDictionary*)dic shellScriptPath:(NSString*)shellScriptPath
{
    NSString *applicationPath = [dic valueForKey:kOpenFileWithApplictionPath];
    NSArray *filePaths = [dic valueForKey:kOpenFileWithFilePathArray];

    NSMutableArray *arguments = [NSMutableArray arrayWithCapacity:0];
    [arguments addObject:shellScriptPath];

    if (applicationPath && applicationPath.length > 0)
    {
        [arguments addObject:@"-a"];
        [arguments addObject:applicationPath];
    }
    if (filePaths && filePaths.count > 0)
    {
        for (NSString *path in filePaths)
        {
            [arguments addObject: path];
        }
    }

    NSTask *task = [[NSTask alloc] init];
    task.launchPath = @"/bin/sh";
    task.arguments = [NSArray arrayWithArray:arguments];
    task.currentDirectoryPath = [[NSBundle  mainBundle] resourcePath];
    NSPipe *outputPipe = [NSPipe pipe];
    [task setStandardOutput:outputPipe];
    [task setStandardError:outputPipe];
    NSFileHandle *readHandle = [outputPipe fileHandleForReading];

    [task launch];
    [task waitUntilExit];

    NSData *outputData = [readHandle readDataToEndOfFile];
    NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
    NSLog(@"Victor-Debug : \n%@",outputString);
    return outputString;
}






//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

#pragma mark - 第二种方法

- (IBAction)useCoreService:(id)sender
{
    NSArray *paths = [NSArray arrayWithObjects:_filePathField.stringValue,nil];


    NSString *appPath = _appPathField.stringValue;

    [OpenFileWithApplication openItems:paths withApplicationPath:appPath open:YES applyToAll:YES];

}



@end
